package com.qf.dao;

import com.qf.pojo.User;
import com.qf.utils.DBUtil;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author Wy
 * @version V1.0
 * @Project javaWeb项目
 * @Package com.qf.dao
 * @Description:
 * @Date 2022/5/21 10:44
 */
public class UserDaoImpl implements UserDao {
    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDateSource());


    /**
     * 根据用户名查询用户
     * @param userName
     * @return
     */
    @Override
    public User findByUserName(String userName) {
              String sql="select * from tb_user where username=?";
        try {
         return    queryRunner.query(sql,new BeanHandler<User>(User.class),userName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加用户操作
     * @param user
     * @return
     */

    @Override
    public int addUser(User user) {
        String sql="insert into tb_user (username,password,email,gender,status,role,code) value(?,?,?,?,?,?,?)";
        try {
            int update = queryRunner.update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getGender(), user.getStatus(), user.getRole(), user.getCode());
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
